<template>
  <div class="pd-20">
    <el-button type="primary"
               @click="handleOption('add', null)">新 增</el-button>
    <el-button type="warning"
               @click="backPage">返 回</el-button>
    <element-table class="mt-20"
                   :columns="columns"
                   :data="tableData"
                   v-loading="loading"
                   pager
                   :pageTotal="pageTotal"
                   :pageIndex="pageIndex"
                   :pageSize="pageSize"
                   @handleChangePage="handleChangePage">
      <template v-slot:option="{row}">
        <el-button type="primary"
                   @click="handleOption('edit', row)">编 辑</el-button>
        <el-button type="danger"
                   @click="handleOption('del', row)">删 除</el-button>
      </template>
    </element-table>
    <diseases-dialog v-if="diseasesDialog"
                     :visible.sync="diseasesDialog"
                     :appointmentId="appointmentId"
                     :dataObj="dataObj"></diseases-dialog>
    <p align="center">
      <el-button type="warning"
                 @click="backPage">返 回</el-button>
    </p>
  </div>
</template>
<script>
  import ElementTable from '@/base/ElementTable'
  import diseasesDialog from './diseasesDialog'
  export default {
    name: 'diseasesList', // 关联病种
    data () {
      return {
        title: '关联病种',
        loading: false,
        diseasesDialog: false,
        pageIndex: 1,
        pageSize: 20,
        tableData: [],
        columns: [
          {
            label: '病种名称',
            prop: 'diseaseName',
            width: 300,
          }, {
            label: '知识点',
            prop: 'diseaseKnowledge'
          }, {
            label: '前端排序',
            prop: 'sortNum',
            align: 'center',
            width: 100
          }, {
            label: '创建时间',
            prop: 'createTime',
            align: 'center',
            width: 150,
            formatter: (row, cellValue) => {
              return this.commonFunction.timestampFormat(new Date(row.createTime), 'yyyy-MM-dd hh:mm:ss')
            }
          }, {
            label: '操作',
            prop: 'option',
            align: 'center',
            isSlot: true,
            width: 200
          }
        ],
        pageTotal: 0,
        appointmentId: Number(this.$route.query.id),
        dataObj: {}
      }
    },
    methods: {
      async getList () {
        this.loading = true;
        const { data: res } = await this.post(this.api.promote.getDisease, {
          appointmentId: this.appointmentId,
          pageIndex: this.pageIndex - 1,
          pageSize: this.pageSize
        });
        if (res.code === '200') {
          this.loading = false;
          this.tableData = res.object;
          this.pageTotal = res.totalNum;
        }
      },
      backPage () {
        this.$router.push({ name: 'activityReserve' });
      },
      async handleOption (op, row) {
        switch (op) {
          case 'add':
            this.dataObj = null;
            this.diseasesDialog = true;
            break;
          case 'edit':
            this.dataObj = Object.assign({}, row);
            this.diseasesDialog = true;
            break;
          case 'del':
            this.$confirm('确认删除？')
              .then(async _ => {
                const { data: res } = await this.post(this.api.promote.delDisease, {
                  id: row.id
                })
                if (res.code === '200') {
                  this.$message.success('删除成功!');
                  this.getList();
                }
              })
              .catch(_ => { });
            break;
          case 'detail':
            this.$router.push({ name: 'reserveDetail', query: { routeParams: JSON.stringify(row) } });
            break;
        }
      },
      // 分页
      handleChangePage (page) {
        this.pageIndex = page;
        this.getList();
      },
    },
    created () {
      this.getList();
    },
    components: {
      'element-table': ElementTable,
      'diseases-dialog': diseasesDialog,
    }
  }
</script>
<style scoped lang="scss">
</style>